#include <stdio.h>

int main() {
    int number;
    int days;
    scanf("%d", &number);
    scanf("%d", &days);

    int item[number];
    int prices[number][days];
    for(int i = 0; i < number; i++) {
        scanf("%d", &item[i]);
    }
    for(int i = 0; i < number; i++) {
        for(int j = 0; j < days; j++) {
            scanf("%d", &prices[i][j]);
        }
    }

    int totalProfit = 0;
    for(int i = 0; i < number; i++) {
        int buyDay = 0;
        int profit = 0;
        // 寻找所有买卖点
        while(buyDay < days - 1) {
            // 寻找一轮买卖点
            while(buyDay < days - 1 && prices[i][buyDay] >= prices[i][buyDay + 1]) {
                buyDay++;
            }
            int priceBuy = prices[i][buyDay];
            int sellDay = buyDay + 1;
            while(sellDay < days && prices[i][sellDay] >= prices[i][sellDay - 1]) {
                sellDay++;
            }
            int priceSell = prices[i][sellDay - 1];

            // 只有找到买卖点才更新利润
            if (sellDay - 1 > buyDay) {
                profit = (prices[i][sellDay - 1] - prices[i][buyDay]) * item[i];
                totalProfit += profit;
            }
            buyDay = sellDay;
        }
    }

    printf("%d\n", totalProfit);
    return 0;
}